package com.yanqu.road.dataHelper.action.excel;

import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.manager.ActivityMgr;
import com.yanqu.road.dataHelper.manager.BaseMgr;
import com.yanqu.road.dataHelper.manager.DataMgr;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author xx
 * @Date 2020/11/11
 */
public class Touhu extends BaseRepair {

    public Touhu(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    //触发礼包一条龙三个日志
    public void go1(){
        List<Map<String, String>> listMap = new ArrayList<>();
        int activityId = 9007986;
//        activityId = 9877490;
        BaseMgr.getMgr(ActivityMgr.class).initActivity(activityId);
        listMap = getLog1(activityId);
        listMap = getLog2(activityId,listMap);
        listMap = getLog3(activityId,listMap);
        listMap = getLog4("2020-11-10","2020-11-13",listMap);
        listMap = getLog5(activityId,listMap);
        DataMgr.addData(listMap);
    }

    private List<Map<String, String>> getLog1(int activityId) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT *,max(`value`+0) as integral FROM t_u_activity_condition " +
                " WHERE activityId = ? GROUP BY userId";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("积分",checkNullToEmpty(rs.getString("integral")));
                dataList.add(temp);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataList;
    }

    private List<Map<String, String>> getLog2(int activityId,List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,activityId,count(1) as times FROM t_l_throwing_pot_log " +
                " WHERE activityId = ? GROUP BY userId";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            Map<String,Map<String,String>> resMap = new ConcurrentHashMap<>();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("times",checkNullToEmpty(rs.getString("times")));
                resMap.put(checkNullToEmpty(rs.getString("userId")),temp);
            }
            for(Map<String,String> data : idataList){
                if(resMap.containsKey(data.get("userId"))){
                    data.put("投壶次数",resMap.get(data.get("userId")).get("times"));
                }else{
                    data.put("投壶次数","0");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

    private List<Map<String, String>> getLog3(int activityId,List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT userId,activityId,count(1) as times FROM t_l_activity_disperse_gold_log " +
                " WHERE activityId = ? GROUP BY userId";
        try {
            call = getGameServer().getLogConnection().prepareCall(sql);
            int i=1;
            call.setInt(i++,activityId);
            rs = call.executeQuery();
            Map<String,Map<String,String>> resMap = new ConcurrentHashMap<>();
            while (rs.next()) {
                Map<String,String> temp = new ConcurrentHashMap<>();
                temp.put("serverId",String.valueOf(getGameServer().getServerInfo().getServerId()));
                temp.put("userId",checkNullToEmpty(rs.getString("userId")));
                temp.put("activityId",checkNullToEmpty(rs.getString("activityId")));
                temp.put("times",checkNullToEmpty(rs.getString("times")));
                resMap.put(checkNullToEmpty(rs.getString("userId")),temp);
            }
            for(Map<String,String> data : idataList){
                if(resMap.containsKey(data.get("userId"))){
                    data.put("送箭次数",resMap.get(data.get("userId")).get("times"));
                }else{
                    data.put("送箭次数","0");
                }
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

    private List<Map<String, String>> getLog4(String dateStart,String dateEnd,List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT goods,`count`,price FROM t_l_buy " +
                " WHERE goods = '120061=1' AND userId = ? AND `time` >= ? AND `time` <= ?";
        try {
            for(Map<String,String> one : idataList){
                call = getGameServer().getLogConnection().prepareCall(sql);
                call.setString(1, one.get("userId"));
                call.setString(2, dateStart);
                call.setString(3, dateEnd);
                rs = call.executeQuery();
                Property price = new Property();
                while (rs.next()){
                    String priceStr = rs.getString("price");
                    Property tempPro = PropertyHelper.parseStringToProperty(priceStr);
                    tempPro.rideProperty(rs.getInt("count"));
                    price.addProperty(tempPro);
                }
                String price101000 = price.getCountByGoodsId(101000).toString();
                one.put("购买花费元宝",price101000);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }

    private List<Map<String, String>> getLog5(int activityId,List<Map<String,String>> idataList) {
        List<Map<String, String>> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "SELECT * FROM t_u_activity_condition " +
                " WHERE activityId = ? AND userId = ? AND isGetReward = 1";
        try {
            for(Map<String,String> one : idataList){
                call = getGameServer().getConnection().prepareCall(sql);
                call.setInt(1, activityId);
                call.setString(2, one.get("userId"));
                rs = call.executeQuery();
                Property reward = new Property();
                while (rs.next()){
                    int conditionId = rs.getInt("conditionId");
                    ActivityConditionInfo activityConditionInfo = ActivityMgr.getActivityConditionInfo(activityId,conditionId);
                    if(activityConditionInfo != null){
                        reward.addProperty(activityConditionInfo.getReward());
                    }else{
                        getLogger().error("找不到"+activityId+"-"+conditionId);
                    }
                }
                String price101000 = reward.getCountByGoodsId(101000).toString();
                one.put("活动获得元宝数",price101000);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return idataList;
    }
}
